from django.db import models
from utils.base import BaseModel
from apps.interfaces.models import Api


class Env(BaseModel):
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    name = models.CharField(verbose_name='环境名称', max_length=20, unique=True, error_messages={'unique': '环境名称已存在'})
    service = models.ManyToManyField('Service', through='EnvService')
    plant = models.ManyToManyField('Plant', through='EnvPlant')
    db = models.ManyToManyField('Db', through='EnvDb')

    class Meta:
        db_table = 'tb_env'
        verbose_name = '环境表'
        verbose_name_plural = verbose_name


class EnvGlobalParams(BaseModel):
    env = models.ForeignKey(Env, on_delete=models.SET_NULL, verbose_name='所属环境', null=True)
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    name = models.CharField(verbose_name='变量名称', max_length=50)
    value = models.CharField(verbose_name='变量值', max_length=200)
    remark = models.CharField(verbose_name='变量备注', max_length=200, null=True)

    class Meta:
        db_table = 'tb_env_params'
        verbose_name = '环境全局变量表'
        verbose_name_plural = verbose_name


class EnvService(BaseModel):
    env = models.ForeignKey(Env, on_delete=models.SET_NULL, verbose_name='所属环境', null=True)
    service = models.ForeignKey('Service', on_delete=models.SET_NULL, verbose_name='所属后端服务', null=True)
    host = models.URLField(verbose_name='后端域名')

    class Meta:
        db_table = 'tb_env_service'
        verbose_name = '环境服务表'
        verbose_name_plural = verbose_name


class EnvPlant(BaseModel):
    env = models.ForeignKey(Env, on_delete=models.SET_NULL, verbose_name='所属环境', null=True)
    plant = models.ForeignKey('Plant', on_delete=models.SET_NULL, verbose_name='所属前端服务', null=True)
    host = models.URLField(verbose_name='前端域名')

    class Meta:
        db_table = 'tb_env_plant'
        verbose_name = '环境平台表'
        verbose_name_plural = verbose_name


class Service(BaseModel):
    project = models.ForeignKey('projects.Project', on_delete=models.SET_NULL, null=True)
    name = models.CharField(verbose_name='服务名称', max_length=20)

    class Meta:
        db_table = 'tb_service'
        verbose_name = '后端微服务表'
        verbose_name_plural = verbose_name


class Headers(BaseModel):
    project = models.ForeignKey('projects.Project', on_delete=models.SET_NULL, null=True)
    name = models.CharField(verbose_name='请求体配置名称', max_length=20)
    value = models.JSONField(verbose_name='请求头值')

    class Meta:
        db_table = 'tb_headers'
        verbose_name = '后端请求头配置'
        verbose_name_plural = verbose_name


class Plant(BaseModel):
    project = models.ForeignKey('projects.Project', on_delete=models.SET_NULL, null=True
                                )
    name = models.CharField(verbose_name='平台名称', max_length=20)

    class Meta:
        db_table = 'tb_plant'
        verbose_name = '平台表'
        verbose_name_plural = verbose_name


class Module(BaseModel):
    name = models.CharField(verbose_name='模块名称', max_length=20)
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    plant = models.ForeignKey(Plant, on_delete=models.PROTECT)

    class Meta:
        db_table = 'tb_module'
        verbose_name = '模块表'
        verbose_name_plural = verbose_name


class Page(BaseModel):
    name = models.CharField(verbose_name='页面名称', max_length=20)
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    url = models.CharField(verbose_name='页面地址', max_length=50)
    module = models.ForeignKey(Module, on_delete=models.PROTECT)

    class Meta:
        db_table = 'tb_page'
        verbose_name = '模块表'
        verbose_name_plural = verbose_name


class EnvDb(BaseModel):

    class SQLType(models.IntegerChoices):
        MYSQL = 1, 'mysql'
        POSTGRESQL = 2, 'postgresql'
    env = models.ForeignKey(Env, on_delete=models.CASCADE, verbose_name='所属环境')
    db = models.ForeignKey('Db', on_delete=models.CASCADE, verbose_name='所属数据库')
    host = models.CharField(verbose_name='数据库地址', max_length=30)
    username = models.CharField(verbose_name='数据库用户名', max_length=20)
    password = models.CharField(verbose_name='数据库密码', max_length=20)
    port = models.CharField(verbose_name='数据库端口号', max_length=20)
    name = models.CharField(verbose_name='连接的数据库名', max_length=20)
    type = models.IntegerField(verbose_name='数据库类型', choices=SQLType.choices, default=SQLType.MYSQL)

    class Meta:
        db_table = 'tb_env_db'
        verbose_name = '数据库环境表'
        verbose_name_plural = verbose_name


class Db(BaseModel):
    name = models.CharField(verbose_name='数据库名称', max_length=20)

    class Meta:
        db_table = 'tb_db'
        verbose_name = '数据库表'
        verbose_name_plural = verbose_name
